package com.xk._01基础篇._10映射Map.set;

import com.xk._01基础篇._10映射Map.map.Map;
import com.xk._01基础篇._10映射Map.map.TreeMap;

import java.util.Comparator;

/**
 * @description: 用写好的TreeMap实现TreeSet </>
 * @author: xu
 * @date: 2022/9/27 9:34
 */
public class TreeSet<E> implements Set<E>{
    TreeMap<E,Object> map;
    public TreeSet(Comparator<E> comparator) {
        map = new TreeMap<>(comparator);
    }
    public TreeSet() {
        this(null);
    }

    @Override
    public int size() {
        return map.size();
    }

    @Override
    public boolean isEmpty() {
        return map.isEmpty();
    }

    @Override
    public void clear() {
        map.clear();
    }

    @Override
    public boolean contains(E element) {
        return map.containsKey(element);
    }

    @Override
    public void add(E element) {
        map.put(element, null);
    }

    @Override
    public void remove(E element) {
        map.remove(element);
    }

    @Override
    public void traversal(Visitor<E> visitor) {
        map.traversal(new Map.Visitor<E, Object>() {
            @Override
            public boolean visit(E key, Object value) {
                visitor.visit(key);
                return false;
            }
        });
    }
}
